package top.chukongxiang.monitorservice.model.exception;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.InvocationTargetException;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/26 14:55
 */
public class FxException extends RuntimeException {

    public FxException() {}

    public FxException(String msg) {
        super(msg);
    }

    public FxException(String msg, Object... params) {
        super(StrUtil.format(msg, params));
    }

    public FxException(Throwable e) {
        super(e);
    }

    public static FxException getFxException(Throwable e) {
        if (e instanceof FxException) {
            return (FxException) e;
        }
        Throwable cause = e.getCause();
        if (cause instanceof FxException) {
            return (FxException) cause;
        }
        if (cause instanceof InvocationTargetException) {
            InvocationTargetException invocationTargetException = (InvocationTargetException) cause;
            Throwable targetException = invocationTargetException.getTargetException();
            return getFxException(targetException);
        }
        return new FxException(e.getMessage());
    }
}
